<template>
    <h1>科室列表</h1>
    科室名称:
    <input type="text" v-model="kname"/>
    科室介绍:
    <input type="text" v-model="kjieshao"/>
    科室创建时间:
    <input type="date" v-model="kcredate"/>
    <input type="button" value="查询" @click="Show"/>

    <table class="table" border="1">
        <thead>
            <tr>
                <td>科室名称</td>
                <td>科室人数</td>
                <td>科室介绍</td>
                <td>科室创建时间</td>
                <td>操作</td>
            </tr>
        </thead>
        <tbody>
            <tr v-for="item in Info">
                <td>{{item.kName}}</td>
                <td>{{item.kSumPeo}}</td>
                <td>{{item.kJieShao}}</td>
                <td>{{item.kCreDate.substring(0,10)}}</td>
                <td>
                    <input type="button" value="删除" @click="Del(item.kId)"/>
                    <input type="button" value="修改" @click="Upd(item.kId)"/>
                </td>
            </tr>
        </tbody>
    </table>
</template>

<script setup lang="ts">
import { ref,onMounted } from "vue";
import axios from "axios";
import { useRouter } from "vue-router";
onMounted(()=>{
    Show();
})

const Info=ref([{
    "kId": 0,
  "kName": "",
  "kCreDate": "",
  "kSumPeo": "",
  "kJieShao": "",
  "isDel": 0
}])

const kcredate=ref("")
const kname=ref("")
const kjieshao=ref("")

const Show=()=>{
    axios.get("https://localhost:7199/api/KeRoom/GetAll",{
        params:{
            kname:kname.value,
            kjieshao:kjieshao.value,
            kcredate:kcredate.value,
        }
    })
    .then(res=>{
        Info.value=res.data
    })
    .catch(error=>{
        console.log(error);
    })
}

const Del=(kid:number)=>{
    if(confirm("确定删除吗？")){
        axios.delete("https://localhost:7199/api/KeRoom/Delete",{
        params:{
             kid:kid
        }
    })
    .then(res=>{
        if(res.data>0){
            alert("删除成功！")
            Show();
        }
        else{
            alert("添加失败")
        }
    })
    .catch(error=>{
        console.log(error);
    })
    }
}

const router=useRouter();
const Upd=(kid:number)=>{
    router.push({
        name:'UpdKeRoom',
        params:{
            kid:kid
        }
    })
}

</script>